import 'package:flutter/cupertino.dart' show CupertinoIcons;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jm_flutter/jm_flutter.dart';

class MyScaffoldTheme {
  static DecorationImage? backgroundDecorationImage;
  static Color? appBarColor;
  // static Color? appBarTextColor;
  static Color? backgroundColor;
  static Widget? backIcon;
}

class MyScaffold extends StatelessWidget {
  const MyScaffold({
    super.key,
    this.title,
    this.centerTitle = true,
    this.hideAppBar = false,
    this.appBarColor,
    this.appBarTextColor,
    this.backgroundColor,
    required this.body,
    this.appBarHeight,
    this.backOnTap,
    this.closeBack = false,
    this.hideDivider = true,
    this.actions,
    this.scroll = false,
    this.floatingActionButton,
    this.i18n = true,
    this.statusBarType,
    this.leading,
    this.resizeToAvoidBottomInset,
    this.bottomSafeArea = true,
    this.padding,
    this.backgroundImage,
    this.bottomNavigationBar,
    this.bottomNavigationBarColor,
    this.backgroundDecorationImage,
    this.backgroundGradient,
    this.appBar,
  });

  final String? title;
  final bool centerTitle;
  final bool hideAppBar;
  final bool hideDivider;
  final bool closeBack;
  final bool scroll;
  final Color? appBarColor;
  final Color? appBarTextColor;
  final Color? backgroundColor;
  final Widget body;
  final double? appBarHeight;

  final VoidCallback? backOnTap;

  final Widget? leading;
  final List<Widget>? actions;
  final Widget? floatingActionButton;
  final bool i18n;
  final StatusBarType? statusBarType;
  final bool? resizeToAvoidBottomInset;
  final bool bottomSafeArea;
  final EdgeInsetsGeometry? padding;
  final String? backgroundImage;
  final Widget? appBar;
  final Widget? bottomNavigationBar;
  final Color? bottomNavigationBarColor;
  final DecorationImage? backgroundDecorationImage;
  final Gradient? backgroundGradient;

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value:
          JmStatusBar(type: statusBarType, navColor: bottomNavigationBarColor),
      child: Scaffold(
        resizeToAvoidBottomInset: resizeToAvoidBottomInset,
        backgroundColor: appBarColor ?? JmColors.appBarBg,
        body: Container(
          decoration: backgroundGradient != null
              ? BoxDecoration(
                  gradient: backgroundGradient,
                )
              : backgroundDecorationImage != null
                  ? BoxDecoration(
                      image: backgroundDecorationImage,
                    )
                  : MyScaffoldTheme.backgroundDecorationImage != null
                      ? BoxDecoration(
                          image: MyScaffoldTheme.backgroundDecorationImage,
                        )
                      : backgroundImage != null
                          ? BoxDecoration(
                              image: DecorationImage(
                                image: AssetImage(backgroundImage!),
                                fit: BoxFit.cover,
                              ),
                            )
                          : null,
          child: Column(
            children: <Widget>[
              hideAppBar
                  ? Container()
                  : Container(
                      color: appBarColor ?? JmColors.appBarBg,
                      child: SafeArea(
                        bottom: false,
                        child: appBar ??
                            Container(
                              alignment: Alignment.center,
                              height: appBarHeight ?? MyScreen.appBarHeight,
                              padding: const EdgeInsets.only(left: 2, right: 2),
                              child: Row(
                                children: <Widget>[
                                  Expanded(
                                    flex: 3,
                                    child: Container(
                                      alignment: Alignment.centerLeft,
                                      child: Visibility(
                                        visible: !closeBack,
                                        child: leading ??
                                            IconButton(
                                              iconSize: 24,
                                              icon: MyScaffoldTheme.backIcon ??
                                                  Icon(
                                                    CupertinoIcons.back,
                                                    //size: 26,
                                                    color: appBarTextColor ??
                                                        JmConfig.appBarTextStyle
                                                            .color,
                                                  ),
                                              onPressed: () {
                                                if (backOnTap != null) {
                                                  backOnTap!();
                                                } else {
                                                  //Get.back();
                                                  Navigator.pop(context);
                                                }
                                              },
                                            ),
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 5,
                                    child: title != null
                                        ? Container(
                                            alignment: Alignment.center,
                                            child: MyText(
                                              title!,
                                              i18n: i18n,
                                              size: JmConfig
                                                  .appBarTextStyle.fontSize,
                                              color: appBarTextColor ??
                                                  JmConfig
                                                      .appBarTextStyle.color,
                                              overflow: TextOverflow.ellipsis,
                                              weight: JmConfig
                                                  .appBarTextStyle.fontWeight,
                                              textAlign: TextAlign.center,
                                              maxLines: 2,
                                            ),
                                          )
                                        : Container(),
                                  ),
                                  Expanded(
                                      flex: 3,
                                      child:
                                          actions != null && actions!.isNotEmpty
                                              ? Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.end,
                                                  children: actions!,
                                                )
                                              : Container()),
                                ],
                              ),
                            ),
                      ),
                    ),
              Visibility(
                visible: !hideDivider,
                child: const Divider(
                  height: 0.8,
                ),
              ),
              Expanded(
                child: scroll
                    ? Container(
                        width: double.infinity,
                        color: backgroundColor ??
                            MyScaffoldTheme.backgroundColor ??
                            JmColors.bg,
                        padding: padding,
                        child: SingleChildScrollView(
                          physics: const BouncingScrollPhysics(),
                          child: body,
                        ),
                      )
                    : Container(
                        color: backgroundColor ??
                            MyScaffoldTheme.backgroundColor ??
                            JmColors.bg,
                        padding: padding,
                        width: double.infinity,
                        child: body,
                      ),
              )
            ],
          ),
        ),
        floatingActionButton: floatingActionButton,
        bottomNavigationBar: bottomNavigationBar,
      ),
    );
  }
}
